﻿using nplJobsCommon.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nplJobsDAL.Select
{
    /// <summary>
    /// Select job category from the database
    /// </summary>
    public class SelectJobCategory : DataAccessBase
    {
        public SelectJobCategory()
        {
            StoredProcedureName = StoredProcedure.Name.spSelectJobCategory.ToString();
        }

        #region GetJobCategory
        public List<JobCategory> GetJobCategory()
        {
            DataBaseHelper dbHelper;
            DataSet dsJobCategory;

            try
            {
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dsJobCategory = dbHelper.Run(base.ConnectionString);
                return GetList(dsJobCategory);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<JobCategory> GetJobCategoryWithJobCount()
        {
            DataBaseHelper dbHelper;
            DataSet dsJobCategory;

            try
            {
                dbHelper = new DataBaseHelper(StoredProcedure.Name.spSelectJobCategoryWithJobCount.ToString());
                dsJobCategory = dbHelper.Run(base.ConnectionString);
                return GetList(dsJobCategory);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<JobCategory> GetGovtJobCategoryWithJobCount()
        {
            DataBaseHelper dbHelper;
            DataSet dsJobCategory;

            try
            {
                dbHelper = new DataBaseHelper(StoredProcedure.Name.spSelectGovtJobCategoryWithJobCount.ToString());
                dsJobCategory = dbHelper.Run(base.ConnectionString);
                return GetList(dsJobCategory);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<JobCategory> GetNewspaperJobCategoryWithJobCount()
        {
            DataBaseHelper dbHelper;
            DataSet dsJobCategory;

            try
            {
                dbHelper = new DataBaseHelper(StoredProcedure.Name.spSelectNewspaperJobCategoryWithJobCount.ToString());
                dsJobCategory = dbHelper.Run(base.ConnectionString);
                return GetList(dsJobCategory);
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region GetList
        private List<JobCategory> GetList(DataSet dsJobCategory)
        {
            List<JobCategory> jobCategories;
            try
            {
                jobCategories = (from d in dsJobCategory.Tables[0].AsEnumerable()
                                 select new JobCategory
                              {
                                  ID = d.Field<int>("ID"),
                                  Caterogy = d.Field<string>("Caterogy")
                              }).ToList();

                return jobCategories;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { }
        }
        #endregion
    }
}

